package com.swak.boot.config.zookeeper;

import static com.swak.boot.Application.APP_LOGGER;

import org.apache.curator.framework.CuratorFramework;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.swak.Constants;
import com.swak.zookeeper.ZookeeperService;

/**
 * 开启 zookeeper.
 * 
 * @author 618lf
 */
@Configuration
@ConditionalOnClass({ CuratorFramework.class, ZookeeperService.class })
@ConditionalOnProperty(prefix = Constants.APPLICATION_PREFIX, name = "enableZk", matchIfMissing = true)
@EnableConfigurationProperties(ZookeeperProperties.class)
public class ZookeeperAutoConfiguration {

	public ZookeeperAutoConfiguration() {
		APP_LOGGER.debug("Loading Zookeeper");
	}

	/**
	 * 创建 zookeeper 客户端
	 * 
	 * @return
	 * @throws InterruptedException
	 */
	@Bean
	public ZookeeperService zookeeperService(ZookeeperProperties properties) throws InterruptedException {
		return ZookeeperBuilder.build(properties);
	}
}
